from model import model
from log import log
from services import MyCache
from utils.markdown import MarkDown


class navlistDao:
    cache = MyCache().fsession()

    @cache.memoize(tag="navlist-id")
    def get(self,nid):
        return model.navlist.objects(id=nid)
    # 列出所有导航

    @cache.memoize(tag="navlist-all")
    def list(self):
        return model.navlist.objects().order_by("level")

    # 获取名字
    def getName(self,id):
        try:
            nav = model.navlist.objects(id=id)
            if not nav == None:
                return nav.get().name
            else:
                return None
        except IOError:
            log.error(IOError)
            return None

    # 清楚所有缓存
    @staticmethod
    def clearCache():
        MarkDown().clearCache()
        cache = MyCache().fsession()
        cache.evict("navlist-id")
        cache.evict("navlist-all")

